#include "StdAfx.h"
#include "TwParticle2D.h"


void  sParticle::SetParticleData(float lifespan,float xSpeed,float ySpeed,float xSpeedUp,float ySpeedUp,int nSize)
{
	m_nLifeSpan = lifespan;
	m_nXSpeed = xSpeed;
	m_nYSpeed = ySpeed;
	m_nSpeedUpX = xSpeedUp;
	m_nSpeedUpY =ySpeedUp ;
	m_nSize = nSize;
	m_nPosX = 250;
	m_nPosY = 250;
	m_BorthTime = GetTickCount();
}
void sParticle::StartRender()
{
	if(g_Render)
	{
		//g_Render->PushSuface("E:\\outcode\\debug\\pp.jpg",FVERTEX2D_FVF,(BYTE*)FINDEX_Default2D,(BYTE*)m_fVertex,0);
		m_bHaveBirth = true;
	}
}
void sParticle::Updata()
{
	if(!m_bHaveBirth)
		return;
	if(IsDeath())
	{
		m_nPosX = 250;
		m_nPosY = 250;
		m_BorthTime = GetTickCount();
	}
	int nSizeHalfe = m_nSize*0.5f;

	int nPassTime = GetTickCount() - m_BorthTime;
	float fTime = nPassTime/1000.f;
	m_nPosX +=    0.5*m_nSpeedUpX*fTime*fTime;
	m_nPosY +=    0.5*m_nSpeedUpY*fTime*fTime;


	m_fVertex[0].x = m_nPosX - nSizeHalfe;
	m_fVertex[0].y = m_nPosY - nSizeHalfe;
	m_fVertex[0].u = 0;
	m_fVertex[0].v = 0;
	m_fVertex[0].color = RandomColor();

	m_fVertex[1].x = m_nPosX - nSizeHalfe;
	m_fVertex[1].y = m_nPosY + nSizeHalfe;
	m_fVertex[1].u = 0;
	m_fVertex[1].v = 1;
	m_fVertex[1].color = RandomColor();

	m_fVertex[2].x = m_nPosX + nSizeHalfe;
	m_fVertex[2].y = m_nPosY - nSizeHalfe;
	m_fVertex[2].u = 1;
	m_fVertex[2].v = 0;
	m_fVertex[2].color = RandomColor();

	m_fVertex[3].x = m_nPosX + nSizeHalfe;
	m_fVertex[3].y = m_nPosY + nSizeHalfe;
	m_fVertex[3].u = 1;
	m_fVertex[3].v = 1;
	m_fVertex[3].color = RandomColor();
}
void sParticle::Render()
{
	if(IsDeath())
		return;

}




TwParticle2D::TwParticle2D(void)
{
}

TwParticle2D::~TwParticle2D(void)
{
}
void TwParticle2D::InitParticle()
{
	for (int n = 0; n < MAX_PARTICLENUM; ++n)
	{
		m_sParticle[n].SetParticleData(50*n,RandomValue(),RandomValue(),RandomValue(),RandomValue(),RandomValue());
		m_sParticle[n].StartRender();
		m_sParticle[n].Updata();
	}
}
void TwParticle2D::Update()
{
	for (int n = 0; n < MAX_PARTICLENUM; ++n)
	{
		m_sParticle[n].Updata();
	}
}
float RandomValue()
{
	int nValue = rand()%251;
	return (nValue/50.f - 2.5f);
}

DWORD RandomColor()
{
	BYTE r = /*rand()%125*/ + 125;
	BYTE g = /*rand()%125*/ + 125;
	BYTE b = /*rand()%125*/ + 125;
	BYTE al =/* rand()%125*/0;

	return al<<24 + r<<16 + g<<8 +b;
}